﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BinaryKits.Zpl.Viewer.WebApi.Models;
using BinaryKits.Zpl.Viewer;
using System.Drawing;
using System.IO;
using System.Text;

namespace ZPLPrint
{
    public partial class Frm_LXWSYYL : Form
    {
        public Frm_LXWSYYL()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            RenderRequestDto request = new RenderRequestDto();
            request.LabelWidth = 102;
            request.LabelHeight = 152;
            request.PrintDensityDpmm = 8;
            string tmp_path = AppDomain.CurrentDomain.BaseDirectory + @"Test\Test.prn";//路径
            string boxStr = getBoxFlie(tmp_path);
            request.ZplData = boxStr;
            RenderZpl(request);

        }
        private RenderResponseDto RenderZpl(RenderRequestDto request)
        {
            IPrinterStorage printerStorage = new PrinterStorage();
            var drawer = new ZplElementDrawer(printerStorage);

            var analyzer = new ZplAnalyzer(printerStorage);
            var analyzeInfo = analyzer.Analyze(request.ZplData);

            var labels = new List<RenderLabelDto>();
            this.imageViewer1.Clear();
            foreach (var labelInfo in analyzeInfo.LabelInfos)
            {
                var imageData = drawer.Draw(labelInfo.ZplElements, request.LabelWidth, request.LabelHeight, request.PrintDensityDpmm);
                Image img = byteArrayToImage(imageData);
                img.Save(AppDomain.CurrentDomain.BaseDirectory + "test.jpg");
                var label = new RenderLabelDto
                {
                    ImageBase64 = Convert.ToBase64String(imageData)
                };
                labels.Add(label);
            }
            this.imageViewer1.LoadImages(AppDomain.CurrentDomain.BaseDirectory, "jpg");

            var response = new RenderResponseDto
            {
                Labels = labels.ToArray(),
                NonSupportedCommands = analyzeInfo.UnknownCommands
            };

            return response;
        }
        /// <summary>
        /// 字节数组生成图片
        /// </summary>
        /// <param name="Bytes">字节数组</param>
        /// <returns>图片</returns>
        private Image byteArrayToImage(byte[] Bytes)
        {
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                Image outputImg = Image.FromStream(ms);
                return outputImg;
            }
        }
        public string getBoxFlie(string _path)
        {
            //读取标签格式内容
            FileStream fs = new FileStream(_path, FileMode.Open);
            fs.Seek(0, SeekOrigin.Begin);
            BinaryReader br = new BinaryReader(fs, Encoding.Default);
            Byte[] b = new Byte[fs.Length];
            //UTF8Encoding temp = new UTF8Encoding(true); 
            ASCIIEncoding temp = new ASCIIEncoding();
            string str = "";
            b = br.ReadBytes(b.Length);
            //str = temp.GetString(b);
            str = Encoding.Default.GetString(b);

            fs.Close();
            br.Close();
            return str;
        }

    }
}
